<?php
namespace Api\Controller;
use Think\Controller; 
class StaticController extends Controller {
	/**
		return data format
		{
			city: [
				{
					category_name: '',
					sum: ''
				},
				{
					category_name: '',
					sum: ''
				}
			]
		}
	**/
	public function collecta(){
		$CrawlStatic = M('CrawlStatic','','DB_CRAWLER_STAT');
		$prcatSta = $CrawlStatic -> field('province,category_name,sum(crawled_num_in_period) as sum')
			-> group('province,category_name') 
			-> order('province')
			-> select();
		foreach( $prcatSta as $key => $value ){
			$sub['category_name'] = $value['category_name'];
			$sub['sum'] = $value['sum'];
			$data[$value['province']][] = $sub;	
		}
		$this -> ajaxReturn($data);
	}

	/***
		return data format 
		[	
			cities: {
				
			}	
		]	
	**/
	public function collectb(){
		$CrawlStatic = M('CrawlStatic','','DB_CRAWLER_STAT');
		$prcatSta = $CrawlStatic -> field('province,category_name,sum(crawled_num_in_period) as sum')
			-> group('province,category_name') 
			-> select();

		$provinces = C('provinces');
		$categories = C('categories');
		
		//创建格式
		foreach($provinces as $province){
			$temp = array();
			foreach($categories as $category){
				$temp[$category] = '';	
			}
			$format[$province] = $temp;
		}
		//填充数据
		foreach($prcatSta as $key => $value){
			$format[$value['province']][$value['category_name']] = $value['sum'];	
		}
		$data['provinces'] = $provinces;
		$data['categories'] = $categories;
		$data['collections'] = $format;
		$ret['status'] = true;
		$ret['count'] = 0;
		$ret['data'] = $data;
		$this -> ajaxReturn($ret);
	}
		
	//省份类别实时数据
	public function realtimestat(){
		$province = I('get.pro','全部');
		$category = I('get.cate','全部');
		$pageno = I('get.pageno');
		$pagesize = C('PAGESIZE');
		
		$date_format = '%Y-%m-%d %H';
		if($province != '全部'){
			$where['province'] = $province; 
		}
		if($category != '全部'){
			$where['category_name'] = $category;
		}
		$CrawlStatic = M('CrawlStatic','','DB_CRAWLER_STAT');
		$data = $CrawlStatic -> field('date_format(time_stamp,"%Y-%m-%d %H:00") as times,sum(crawled_num_in_period) as total')
			-> where($where)
			-> group('date_format(time_stamp,"%Y-%m-%d %H")')
			-> order('time_stamp desc')
			-> select();
		
		if(count($data) > 0){
			$ret['status'] = true;	
			$ret['count'] = count($data['realtimedata']);
			$ret['data'] = $data;
		}else if(count($data) == 0){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Empty!';
		}else {
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Query Failed!';
		}	
		
		$ret['ext']['provinces'] = C('provinces');
		$ret['ext']['categories'] = C('categories');
		array_unshift($ret['ext']['provinces'],"全部");
		array_unshift($ret['ext']['categories'],"全部");


		$this->ajaxReturn($ret);
	}

	//所有
	public function detail(){
		$pageno = I('get.pageno',1);
		$province = I('get.pro','全部');
		$category = I('get.cate','全部');
		$pagesize = C('PAGESIZE');
		
		$categories = C('categories');
		$provinces = C('provinces');

		array_unshift($categories,"全部");
		array_unshift($provinces,"全部");

		$CrawlStatic = M('CrawlStatic','','DB_CRAWLER_STAT');
		if($province != '全部'){
			$where['province'] = $province; 		
		}
		if($category != '全部'){
			$where['category_name'] = $category; 		
		}
		$totals = $CrawlStatic -> field('count(*) as total') -> where($where) -> select();
		$data['totalCounts'] = $totals[0]['total'];
		$data['pageSize'] = $pagesize;
		$data['detail'] = $CrawlStatic -> field('province,category_name,crawled_num_in_period,time_stamp') -> where($where) -> order('time_stamp desc')  -> limit($pagesize) -> page($pageno) -> select();
		$data['detail'] = count($data['detail']) > 0 ? $data['detail'] : false;
		if(count($data['detail']) > 0){
			$ret['status'] = true;	
			$ret['count'] = count($data);
			$ret['data'] = $data;
		}else if(count($data['detail']) == 0){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Empty!';
		}else {
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Query Failed!';
		}
		$ret['ext']['categories'] = $categories;
		$ret['ext']['provinces'] = $provinces;
		$this->ajaxReturn($ret);
	}
}
?>
	
